package com.sdwz.library.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdwz.commonutil.Result;
import com.sdwz.library.entity.BookCategory;
import com.sdwz.library.entity.vo.BookCategoryVo;
import com.sdwz.library.service.BookCategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 图书类别表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-03-21
 */
@RestController
@RequestMapping("/admin/library/category")
public class BookCategoryController {

    @Autowired
    BookCategoryService bookCategoryService;

    /**
     * 查询所有图书类别
     * @return
     */
    @GetMapping("list")
    public Result getCategoryList(){
        QueryWrapper<BookCategory> wrapper = new QueryWrapper<>();
        //按sort排序
        wrapper.orderByAsc("sort");
        List<BookCategory> categoryList = bookCategoryService.list(wrapper);
        List<BookCategoryVo> categories = new ArrayList<>();
        for (BookCategory category :categoryList){
            BookCategoryVo bookCategoryVo = new BookCategoryVo();
            bookCategoryVo.setCateName(category.getCateName());
            bookCategoryVo.setId(category.getId());
            bookCategoryVo.setSort(category.getSort());
            categories.add(bookCategoryVo);
        }
        return Result.ok(categories);
    }

    /**
     * 添加类别
     * @param bookCategoryVo
     * @return
     */
    @PostMapping("/saveCategory")
    public Result addCategory(@RequestBody BookCategoryVo bookCategoryVo ){
        BookCategory bookCategory = new BookCategory();
        bookCategory.setCateName(bookCategoryVo.getCateName());
        bookCategory.setSort(bookCategoryVo.getSort());
        boolean flag = bookCategoryService.save(bookCategory);
        if (!flag){
            return Result.fail();
        }
        return Result.ok();
    }

    /**
     * 逻辑删除图书类别
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public Result removeCategory(@PathVariable String id){
        boolean flag = bookCategoryService.removeById(id);
        if (!flag){
            return Result.fail().message("删除失败");
        }
        return Result.ok();
    }

    /**
     * 修改图书类别
     * @param bookCategoryVo
     * @return
     */
    @PostMapping("updateCategory")
    public Result updateCategory(@RequestBody BookCategoryVo bookCategoryVo) {
        BookCategory bookCategory = new BookCategory();
        bookCategory.setId(bookCategoryVo.getId());
        bookCategory.setCateName(bookCategoryVo.getCateName());
        bookCategory.setSort(bookCategoryVo.getSort());
        boolean flag = bookCategoryService.updateById(bookCategory);
        if (!flag){
            return Result.fail().message("修改失败");
        }
        return Result.ok();
    }
}

